var express = require('express');
var router = express.Router();
var Users = require("../models/users");
var crypto = require('crypto');


function decrypt(str, secret) {
    var decipher = crypto.createDecipher('aes192', secret);
    var dec = decipher.update(str, 'hex', 'utf8');
    dec += decipher.final('utf8');
    console.log("dec is: "+dec);
    return dec;
}
//登入
router.post('/login', function(req, res, next) {

    var param = {
        userName:req.body.username,
        userPwd:decrypt(req.body.password ,"pwd"),
    };
    console.log(req.body.password);
    Users.findOne(param,function(err,doc){

        if(err){
            res.josn({
                status:"1",
                msg:err.message
            })
        }else{

            if(doc != null){
                console.log(doc)
                if(doc.freeze){
                    res.json({
                        status:"2",
                        msg:"账号已冻结",
                    })
                }else{
                    res.json({
                        status:"0",
                        msg:"",
                        result:{
                            userName:doc.userName,
                            authority:doc.authority
                        }
                    })
                }
                // req.session.user = doc;

            }else{
                res.json({
                    status:"2",
                    msg:"密码或账号有误",

                })
            }
        }
    })
});

//修改密码
router.post("/pwdEdit",function(req,res,next){
    var param = {
        userName:req.body.userName,
        userPwd:decrypt(req.body.addPwd ,"pwd"),
    };

    Users.update({
        userName:param.userName,
    },{
        $set:{'userPwd':param.userPwd}
    },function(err,doc){
        if(err){
            res.json({
                status:"1",
                msg:err.message,
                result: ""
            })
        }else{
            if(doc){
                res.json({
                    status:"0",
                    msg:"修改成功",
                    result: "suc"
                })
            }
        }
    });
});

//获取用户数据
router.get('/userList', function(req, res, next) {
    // let page = parseInt(req.param("page"));
    // let pageSize = parseInt(req.param("pageSize"));
    // let sort = req.param("sort");
    // let skip = (page-1)*pageSize;
    // let params = {};
    // let goodsModel = Goods.find(params).skip(skip).limit(pageSize);
    //
    // goodsModel.sort({"salePrice" : sort});
    let model = Users.find();
    model.exec(function(err,result){
        if(err){
            res.json({
                status:"1",
                msg:err.message
            })
        }else{
            res.json({
                status:"0",
                msg:"",
                result:{
                    count:result.length,
                    list:result
                }
            })
        }
    });
});

//添加用户数据
router.post('/addUser', function(req, res, next) {
    var param = {
        userName:req.body.username,
        userPwd:req.body.password,
        authority:req.body.authority,
        controller: false,
        freeze:false,
    };
    Users.findOne({userName:req.body.username},function(err,result) {
        if (err) {
            res.json({
                status: "1",
                msg: err.message,
                result: ""
            })
        } else {

            if(result != null){
                res.json({
                    status:"2",
                    msg:"用户名已存在",
                })
            }else{
                Users.create( param ,function(err,doc){
                    if(err){
                        res.json({
                            status:"1",
                            msg:err.message,
                            result: ""
                        })
                    }else{
                        if(doc){
                            res.json({
                                status:"0",
                                msg:"添加成功",
                                result: "suc"
                            })
                        }
                    }
                })
            }
        }
    })
});

//修改用户数据
router.post('/editUser', function(req, res, next) {
    var param = {
        userName:req.body.username,
        userPwd:req.body.password,
        authority:req.body.authority,

    };
    Users.findOne({userName:req.body.username},function(err1,result) {
        if (err1) {
            res.json({
                status: "1",
                msg: err1.message,
                result: ""
            })
        } else {
            if(result == null || result._id == req.body.id){
                Users.update({
                    _id:req.body.id
                },{
                    $set:param
                },function(err,doc){
                    if(err){
                        res.json({
                            status:"1",
                            msg:err.message,
                            result: ""
                        })
                    }else{
                        if(doc){
                            res.json({
                                status:"0",
                                msg:"修改成功",
                                result: "suc"
                            })
                        }
                    }
                })
            } else{
                res.json({
                    status:"2",
                    msg:"用户名已存在",
                })
            }
        }
    })
});

//冻结用户
router.post('/freezeUser', function(req, res, next) {
    Users.update({
        _id:req.body.id
    },{
        $set:{'freeze':req.body.freeze}
    },function(err,doc){
        if(err){
            res.json({
                status:"1",
                msg:err.message,
                result: ""
            })
        }else{
            if(doc){
                res.json({
                    status:"0",
                    msg:"修改成功",
                    result: "suc"
                })
            }
        }
    });
});

//删除用户
router.post('/delUser', function(req, res, next) {
    Users.remove({"_id":req.body.id}, function (err,doc){
        if(err){
            res.json({
                status:"1",
                msg:err.message,
                result: ""
            })
        }else{
            if(doc){
                res.json({
                    status:"0",
                    msg:"删除成功",
                    result: "suc"
                })
            }
        }
    });
});

//获取某一条数据
router.post('/getOneUser', function(req, res, next) {

    Users.find({ //查找一条
        userName:req.body.userName,
    },(err, doc)=>{
        if(err){
            res.json({
                status:"1",
                msg:err.message
            })
        }else{
            res.json({
                status:"0",
                msg:"",
                result:{
                    list:doc
                }
            })
        }
    })
});

module.exports = router;
